/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.functions;

import com.badlogic.gdx.Gdx;

/**Class for Math functions.*/
public class FunctionsMath {
	/**
	  * Generates random numbers between a minimum and a maximum.
	  * @param min Minimum.
	  * @param max Maximum.
	  * @return Random number between a minimum and a maximum.
	  */
	 
	 public int randomInt( int min, int max )
	 {
		 return (int)(Math.rint( (randomFloat(min, max)*10)/10 ));
	 }
	 
	 /**
	  * Generates random numbers between a minimum and a maximum.
	  * @param min Minimum.
	  * @param max Maximum.
	  * @return Random number between a minimum and a maximum.
	  */
	 
	 public float randomFloat( float min, float max )
	 {
		 return (float)(Math.random() * (max - min)) + min;
	 }
	 
	 /**
	  * Gets the angle nearest.
	  * @param AngleIni Initial angle.
	  * @param AngleFin Final angle.
	  * @param Increment Increment of angle.
	  * @return Angle nearest.
	  */

	 public float getNearAngle( float AngleIni, float AngleFin, int Increment )
	 { 
	    if( (AngleIni < AngleFin) && ((AngleFin - AngleIni) > 180) )
	    	AngleIni += 360;

		if( (AngleIni > AngleFin) && ((AngleIni - AngleFin) > 180) )
			AngleIni -= 360 ;

		if( AngleIni < AngleFin )
	    {
			AngleIni += Increment;
			if (AngleIni > AngleFin)
				AngleIni = AngleFin;
		}else{
			AngleIni -= Increment;
			if (AngleIni < AngleFin)
				AngleIni = AngleFin;
		}

		if( AngleIni < 0 )
	     return AngleIni + 360;
	    
		if( AngleIni >= 360 )
	     return AngleIni - 360;
	    
	    
		return AngleIni;
	 }

	 /**
	  * Get an angle between 0 and 360 degrees.
	  * @param angle Initial angle.
	  * @return Angle between 0 and 360 degrees.
	  */
		
	 public float getNormAngle( float angle )
	 {
		 			int NAngle = (int)(angle*1000);
					
					if( NAngle < 0)  
						 NAngle = (NAngle % 360000) + 360000;
					else
						 NAngle = (NAngle % 360000);
					
					return (NAngle/1000.0f);
	 }
		
	 /**
	  * Gets the distance between two points.
	  * @param x0 X0 Coordinate.
	  * @param y0 Y0 Coordinate.
	  * @param x1 X1 Coordinate.
	  * @param y1 Y1 Coordinate.
	  * @return Distance between two points. 
	  */
		
	 public float getDist2d( float x0, float y0, float x1, float y1 )
	 {	  
		   float dx = Math.abs( x1 - x0 );
		   float dy = Gdx.graphics.getHeight() - Math.abs( y1 - y0 );
		   
		   return (float) Math.sqrt( (dx * dx) + (dy * dy) ); 
	 }
	    
	 
		 
	 /**
	  * Gets the from a given distance in the X component.
	  * @param angle Angle.
	  * @param distance Distance.
	  * @return Distance in the X component.
	  */
		 
	 public float getDistX2d( float angle, int distance )
	 {
		  return (float) (distance * Math.cos(Math.toRadians(angle)));  
	 }
		 
	 /**
	  * Gets the from a given distance in the Y component.	 
	  * @param angle Angle.
	  * @param distance Distance.
	  * @return Distance in the Y component.
	  */
	 	 
	 public float getDistY2d( float angle, int distance )
	 {
		  return  (float) (distance * Math.sin(Math.toRadians(angle)));
	 }
		 
	 /**
	  * Gets the angle between two points.
	  * @param x0 X0 Coordinate.
	  * @param y0 Y0 Coordinate.
	  * @param x1 X1 Coordinate.
	  * @param y1 Y1 Coordinate.
	  * @return Angle between two points.
	  */
		 
	 public float getAngle2d( float x0, float y0, float x1, float y1 )
	 {
		  float dx = x1 - x0;
		  float dy = Gdx.graphics.getHeight() - y1 - y0;

		  if( dx == 0 )
		   return dy > 0 ? 90 : -90;

		  float angle = (float) -((Math.atan(dy / dx) * 180) / Math.PI );

		  if( dx < 0 ) 
		    if( dy < 0 ) 
		     angle -= 180;
		    else
		     angle += 180;

		  return (-angle);
	 }
}
